package com.mak001.lite.multiSkill.subs;

import org.rsbuddy.tabs.Inventory;
import org.rsbuddy.widgets.Bank;

import com.mak001.lite.multiSkill.Methods;
import extras.mak001.Time;
import com.mak001.lite.multiSkill.subScript;
import com.rsbuddy.event.events.MessageEvent;
import com.rsbuddy.script.methods.Calculations;
import com.rsbuddy.script.methods.GroundItems;
import com.rsbuddy.script.methods.Objects;
import com.rsbuddy.script.methods.Players;
import com.rsbuddy.script.methods.Skills;
import com.rsbuddy.script.methods.Walking;
import com.rsbuddy.script.util.Random;
import com.rsbuddy.script.wrappers.GameObject;
import com.rsbuddy.script.wrappers.GroundItem;
import com.rsbuddy.script.wrappers.Tile;

public class FireMakerLite extends subScript {

	private int log;
	private int tinder = 590;
	String logName;
	String status = "";
	private int numFires;
	private boolean usedTinder;
	private int ge = 0;
	private boolean didTile = false;
	private Tile fireTile;
	Tile currTile;
	private int currInt;
	private Time time = new Time();
	private boolean canSwitch = false;

	@Override
	public void execute() {
		if (Methods.GrandExchange.contains(Players.getLocal().getLocation())) {
			if (ge == 0 || ge == -1 || didTile == false) {
				ge = Random.nextInt(1, 4);
				didTile = true;
			}
		}
		if (!Inventory.contains(log) && Players.getLocal().getAnimation() == -1) {
			status = "Banking";
			if (canSwitch != true)
				canSwitch = true;
			if (Calculations.distanceTo(bankTile()) < 10
					&& bankTile().isOnScreen()) {
				if (Bank.isOpen()) {
					if (Bank.getCount(log) > 0) {
						if (usedTinder == true) {
							usedTinder = false;
						} else {
							fireTile = null;
							Bank.withdraw(log, 27);
						}
					} else {
						Methods.stopSub();
					}
				} else {
					Bank.open();
				}
			} else {
				Walking.findPath(bankTile()).traverse();
			}
		} else {
			status = "Making Fires";
			if (canSwitch == true)
				canSwitch = false;
			if (fireTile == null) {
				fireTile = startTiles()[Random.nextInt(0, startTiles().length)];
			} else {
				if (Bank.isOpen()) {
					Bank.close();
				} else {
					if (containsMe(fireLane(fireTile))) {
						if (Objects.getTopAt(Players.getLocal().getLocation()) != null
								&& Objects.getTopAt(
										Players.getLocal().getLocation())
										.getType().equals(
												GameObject.Type.INTERACTIVE)
								&& !Players.getLocal().isMoving()) {
							fireTile = null;
						} else {
							if (Players.getLocal().isMoving()
									|| Players.getLocal().getAnimation() == -1) {
								if (logs() == null) {
									light();
								}
							}
						}
					} else {
						currTile = fireLane(fireTile)[0];
						if (fireTile.isOnScreen()) {
							fireTile.click();
						} else {
							Walking.findPath(fireTile).traverse();
						}
					}
				}
			}
		}
	}

	private Tile bankTile() {
		return Methods.getNearestBank().getCentralTile();
	}

	private GroundItem logs() {
		GroundItem h = null;
		for (GroundItem i : GroundItems.getAllAt(Players.getLocal()
				.getLocation())) {
			if (i.getItem().getId() == log) {
				h = i;
			}
		}
		return h;
	}

	public int getCurrentInt() {
		return currInt;
	}

	private void light() {
		if (usedTinder) {
			if (Inventory.useItem(Inventory.getItem(log), Inventory
					.getItem(tinder))) {
				light2();
				currTile = fireLane(fireTile)[currInt + 1];
			}
		} else {
			if (Inventory.useItem(Inventory.getItem(tinder), Inventory
					.getItem(log))) {
				light2();
				currTile = fireLane(fireTile)[currInt + 1];
			}
		}
	}

	private void light2() {
		if (usedTinder == true) {
			usedTinder = false;
		} else {
			usedTinder = true;
		}
		numFires++;
		sleep(900);
	}

	@Override
	public int[] required() {
		int[] req = { tinder };
		return req;
	}

	public void setLog(int i, String s) {
		log = i;
		logName = s;
	}

	private Tile[] startTiles() {
		if (Methods.containsTile(Methods.GrandExchange, bankTile())) {
			return new Tile[] { new Tile(3183, 3485), new Tile(3183, 2484),
					new Tile(3183, 3498), new Tile(3183, 3499),
					new Tile(3180, 3509), new Tile(3180, 3510),
					new Tile(3180, 3474), new Tile(3180, 3473) };
		} else if (Methods.containsTile(Methods.VarrokWest, bankTile())) {
			return new Tile[] { new Tile(3204, 3428), new Tile(3204, 3429),
					new Tile(3204, 3430) };
		} else if (Methods.containsTile(Methods.VarrokEast, bankTile())) {
			return new Tile[] { new Tile(3270, 3430), new Tile(3270, 3429),
					new Tile(3270, 3428) };
		} else if (Methods.containsTile(Methods.FaladorEast, bankTile())) {
			return new Tile[] { new Tile(3032, 3359), new Tile(3032, 3360),
					new Tile(3032, 3361), new Tile(3032, 3362) };
		} else if (Methods.containsTile(Methods.Edgeville, bankTile())) {
			return new Tile[] { new Tile(3105, 3504), new Tile(3110, 3505),
					new Tile(3100, 3487), new Tile(3100, 3486) };
		}

		return null;
	}

	private boolean containsMe(Tile[] ts) {
		boolean b = false;
		Tile me = Players.getLocal().getLocation();
		for (Tile t : ts) {
			if (t.equals(me)) {
				b = true;
			}
		}
		return b;
	}

	private Tile[] fireLane(Tile t) {
		int x = t.getX();
		int y = t.getY();
		return new Tile[] { t, new Tile(x - 1, y), new Tile(x - 2, y),
				new Tile(x - 3, y), new Tile(x - 4, y), new Tile(x - 5, y),
				new Tile(x - 6, y), new Tile(x - 7, y), new Tile(x - 8, y),
				new Tile(x - 9, y), new Tile(x - 10, y), new Tile(x - 11, y),
				new Tile(x - 12, y), new Tile(x - 13, y), new Tile(x - 14, y),
				new Tile(x - 15, y), new Tile(x - 16, y), new Tile(x - 17, y),
				new Tile(x - 18, y), new Tile(x - 19, y), new Tile(x - 20, y),
				new Tile(x - 21, y), new Tile(x - 22, y), new Tile(x - 23, y),
				new Tile(x - 24, y), new Tile(x - 25, y), new Tile(x - 26, y),
				new Tile(x - 27, y), new Tile(x - 28, y) };
	}

	@Override
	public boolean canSwitch() {
		return canSwitch;
	}

	@Override
	public String getStatus() {
		return status;
	}

	@Override
	public Time getTime() {
		return time;
	}

	@Override
	public void messageRecieved(MessageEvent e) {
	}

	@Override
	public String[] paintString() {
		return new String[] { "Fires made: " + numFires };
	}

	@Override
	public int skill() {
		return Skills.FIREMAKING;
	}

}
